﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.Data.SqlClient;

public partial class frmListaDeEmpleados : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    
    
    public override void VerifyRenderingInServerForm(Control control)
    {

    }


    protected void Button1_Click1(object sender, EventArgs e)
    {
        Response.Redirect("frmEmpleados.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        String fecha = DateTime.Now.ToString("dd/MM/yyyy");
        string imagepath = Server.MapPath("Img");

        this.SqlDataSource2.DataSourceMode = SqlDataSourceMode.DataReader;
        SqlDataReader datos = (SqlDataReader)this.SqlDataSource2.Select(DataSourceSelectArguments.Empty);
        iTextSharp.text.Font fontTitle = FontFactory.GetFont("Arial", 18, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
        iTextSharp.text.Font fontHead = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
        iTextSharp.text.Font fontBody = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

        FileStream fs = new FileStream(Server.MapPath("~/") + "listaempleados.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
        Rectangle rec3 = new Rectangle(PageSize.A4.Rotate());
        Document doc = new Document(rec3, 5, 5, 5, 5);
        PdfWriter writer = PdfWriter.GetInstance(doc, fs);

        Console.WriteLine("");

        Paragraph titulo = new Paragraph("LISTADO DE EMPLEADOS AL " + fecha, fontTitle);
        titulo.Alignment = Element.ALIGN_JUSTIFIED;

        doc.Open();
        iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(imagepath + "/logoaf.png");
        doc.Add(logo);
        doc.Add(new Paragraph("  "));
        doc.Add(titulo);
        doc.Add(new Paragraph("  "));
        iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(11);
        table.HorizontalAlignment = 0;
         table.TotalWidth = 792f;
         table.LockedWidth = true;
         float[] widths = new float[] { 54f, 80f, 80f, 88f, 80f, 110f, 110f, 45f, 45f, 40f, 100f };
         table.SetWidths(widths);

        PdfPCell hlegajo = new PdfPCell(new Phrase("LEGAJO", new Font(fontHead)));
        PdfPCell hapellido = new PdfPCell(new Phrase("APELLIDO", new Font(fontHead)));
        PdfPCell hnombre = new PdfPCell(new Phrase("NOMBRE", new Font(fontHead)));
        PdfPCell hdni = new PdfPCell(new Phrase("DNI", new Font(fontHead)));
        PdfPCell htelefono = new PdfPCell(new Phrase("TELEFONO.", new Font(fontHead)));
        PdfPCell hemail = new PdfPCell(new Phrase("E-MAIL", new Font(fontHead)));
        PdfPCell hcalle = new PdfPCell(new Phrase("CALLE", new Font(fontHead)));
        PdfPCell hnro = new PdfPCell(new Phrase("NRO.", new Font(fontHead)));
        PdfPCell hpiso = new PdfPCell(new Phrase("PISO", new Font(fontHead)));
        PdfPCell hdpto = new PdfPCell(new Phrase("DPTO.", new Font(fontHead)));
        PdfPCell hobs = new PdfPCell(new Phrase("OBSERVACIONES", new Font(fontHead)));


        table.AddCell(hlegajo);
        table.AddCell(hapellido);
        table.AddCell(hnombre);
        table.AddCell(hdni);
        table.AddCell(htelefono);
        table.AddCell(hemail);
        table.AddCell(hcalle);
        table.AddCell(hnro);
        table.AddCell(hpiso);
        table.AddCell(hdpto);
        table.AddCell(hobs);

        doc.Add(new Paragraph("  "));
        while (datos.Read())
        {
            String legajo = datos["legajo"].ToString();
            String apellido = datos["apellido"].ToString();
            String nombre = datos["nombre"].ToString();
            String dni = datos["numDocumento"].ToString();
            String telefono = datos["numTelefono"].ToString();
            String email = datos["dirCorreo"].ToString();
            String calle = datos["calle"].ToString();
            String nro = datos["numeroCalle"].ToString();
            String piso = datos["piso"].ToString();
            String dpto = datos["departamento"].ToString();
            String obs = datos["observaciones"].ToString();

            PdfPCell clegajo = new PdfPCell(new Phrase(legajo, new Font(fontBody)));
            PdfPCell capellido = new PdfPCell(new Phrase(apellido, new Font(fontBody)));
            PdfPCell cnombre = new PdfPCell(new Phrase(nombre, new Font(fontBody)));
            PdfPCell cdni = new PdfPCell(new Phrase(dni, new Font(fontBody)));
            PdfPCell ctelefono = new PdfPCell(new Phrase(telefono, new Font(fontBody)));
            PdfPCell cemail = new PdfPCell(new Phrase(email, new Font(fontBody)));
            PdfPCell ccalle = new PdfPCell(new Phrase(calle, new Font(fontBody)));
            PdfPCell cnro = new PdfPCell(new Phrase(nro, new Font(fontBody)));
            PdfPCell cpiso = new PdfPCell(new Phrase(piso, new Font(fontBody)));
            PdfPCell cdpto = new PdfPCell(new Phrase(dpto, new Font(fontBody)));
            PdfPCell cobs = new PdfPCell(new Phrase(obs, new Font(fontBody)));

            table.AddCell(clegajo);
            table.AddCell(capellido);
            table.AddCell(cnombre);
            table.AddCell(cdni);
            table.AddCell(ctelefono);
            table.AddCell(cemail);
            table.AddCell(ccalle);
            table.AddCell(cnro);
            table.AddCell(cpiso);
            table.AddCell(cdpto);
            table.AddCell(cobs);
        }
        doc.Add(table);
        doc.Add(new Paragraph("  "));
        doc.Add(new Paragraph("  "));

        doc.Close();

        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment;filename=listaempleados.pdf");
        Response.TransmitFile(Server.MapPath("~/") + "listaempleados.pdf");


    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["empleado"] = this.GridView1.Rows[this.GridView1.SelectedIndex].Cells[4].Text;
        Response.Redirect("frmEmpleados.aspx");

    }
}